package com.liu.qqRobot.wia.service.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import com.liu.common.content.RedisKeyInterface;
import com.liu.common.entity.vo.WIAGetInvasionsVO;
import com.liu.common.utils.HtmlToImageUtils;
import com.liu.common.utils.QQClientUtils;
import com.liu.common.utils.WIAClientUtils;
import com.liu.qqRobot.wia.service.ListenInInvasionsService;
import freemarker.template.TemplateException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ListenInInvasionsServiceImpl implements ListenInInvasionsService {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 高价值物品列表
     */
    private final List<String> highValueItemList = Arrays.asList("OROKIN催化剂 蓝图", "OROKIN反应堆 蓝图", "Forma蓝图",
            "特殊功能槽连接器");

    @Override
    public void listenIn() {
        List<WIAGetInvasionsVO> wiaGetInvasionsVOList = new ArrayList<>();
        try {
            wiaGetInvasionsVOList = WIAClientUtils.getInvasions();
        } catch (IOException | ClassNotFoundException | InvocationTargetException | IllegalAccessException |
                 NoSuchMethodException e) {
            log.error("获取warfrmae入侵信息失败:{}", e.getMessage(), e);
            return;
        }
        // 还未完成并有高价值物品的入侵任务
        wiaGetInvasionsVOList = wiaGetInvasionsVOList.stream().filter(w -> !w.getCompleted()).filter(w -> {
            boolean result = false;
            List<WIAGetInvasionsVO.Reward.CountedItem> countedItems;
            if (w.getAttacker().getReward() != null) {
                countedItems = w.getAttacker().getReward().getCountedItems();
                for (WIAGetInvasionsVO.Reward.CountedItem countedItem : countedItems) {
                    if (highValueItemList.contains(countedItem.getKey())) {
                        result = true;
                        break;
                    }
                }
            }
            if (w.getDefender().getReward() != null) {
                countedItems = w.getDefender().getReward().getCountedItems();
                for (WIAGetInvasionsVO.Reward.CountedItem countedItem : countedItems) {
                    if (highValueItemList.contains(countedItem.getKey())) {
                        result = true;
                        break;
                    }
                }
            }
            // 判断今天内是否提醒过
            if (result && Boolean.TRUE.equals(redisTemplate.hasKey(RedisKeyInterface.LISTEN_IN_INVASIONS_ID_REDIS_KEY + w.getId()))) {
                result = false;
            } else {
                redisTemplate.opsForValue().set(RedisKeyInterface.LISTEN_IN_INVASIONS_ID_REDIS_KEY + w.getId(), w.getId(), 1, TimeUnit.DAYS);
            }
            return result;
        }).collect(Collectors.toList());
        if (CollUtil.isEmpty(wiaGetInvasionsVOList)) {
            return;
        }
        Map<String, Object> templateParam = new HashMap<>();
        templateParam.put("wiaGetInvasionsVOList", wiaGetInvasionsVOList);
        try {
            HtmlToImageUtils.htmlToImage("warframeInvasionsTemplate.html", templateParam,
                    "src/main/resources/picture/warframeInvasions.png", 2000);
        } catch (IOException | ParserConfigurationException | TemplateException | SAXException e) {
            log.error("获取warfrmae入侵模板失败:{}", e.getMessage(), e);
            return;
        }
        File file = new File("src/main/resources/picture/warframeInvasions.png");
        String encode = Base64.encode(file);
        String msg = "[CQ:image,file=base64://" + encode+ ",type=show,subType=0]";
        QQClientUtils.sendMsg(msg, null, 938354542L, true);
    }

}
